﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Com.CodePlex.SPPower.Data;
using Com.CodePlex.SPPower.Controller;
using Com.CodePlex.SPPower.Model;
using System.Windows.Input;
//using Com.CodePlex.SPPower.View; //should not reference this http://social.msdn.microsoft.com/Forums/is/wpf/thread/3bced3ea-4a97-4456-bfe5-828289bcf971

namespace Com.CodePlex.SPPower.ViewModel {

	//http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-by-step.aspx
	//http://stackoverflow.com/questions/3532924/treeview-multibinding-in-wpf
	//http://stackoverflow.com/questions/1854541/wpf-treeview-mvvm
	//http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4acc1503-53df-42c5-b083-d4b85641d5dd/
	//http://www.albahari.com/threading/part3.aspx
	//http://jfoscoding.blogspot.com/2008/02/treenode-class-to-help-bind-to-treeview.html
	//http://social.msdn.microsoft.com/Forums/en/wpf/thread/257c6905-99bd-4a63-ab8f-4e3bfc72de54
	//lazy loading tree
	//http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

	public class LazyChildViewModel : ViewModel {

	}

	public class LazyChildrenViewModel : ViewModel {

		private LazyChildViewModel DummyChild = new LazyChildViewModel();

        protected LazyChildrenViewModel(LazyChildrenViewModel parent) {
            this.parent = parent;
            Children.Add(DummyChild);
        }

        private LazyChildrenViewModel parent;

		private ObservableCollection<ViewModel> children;
		public ObservableCollection<ViewModel> Children {
			get {
				if (children == null) {
					children = new ObservableCollection<ViewModel>();
				}
				return children;
			}
		}

		private bool isexpanded = false;
		public bool IsExpanded {
			get { return isexpanded; }
			set {
				if (value != isexpanded) {
					isexpanded = value;
					OnPropertyChanged(() => IsExpanded);
				}
				if (isexpanded && parent != null) {
					parent.IsExpanded = true;
				}
				if (HasDummyChild) {
                    RemoveDummy();
					Load();
				}
			}
		}

        public void RemoveDummy() {
            if (HasDummyChild) {
                Children.Remove(DummyChild);
            }
        }

		public bool HasDummyChild {
			get { return Children.Count == 1 && Children[0] == DummyChild; }
		}

		protected virtual void Load() {

		}
	}
}